Organization: Northwestern University, Evanston IL
Message-ID: <4e5q5p$ekt@news.acns.nwu.edu>
References: <4e5dp2$m4i@sun1000.pwr.wroc.pl>
Reply-To: sjudd@nwu.edu (Stephen Judd)
NNTP-Posting-Host: merle.acns.nwu.edu
In article <4e5dp2$m4i@sun1000.pwr.wroc.pl>,
A Member of Elysium <st103@sco.zsi.pwr.wroc.pl> wrote:
>Hi!
>
>I'm going to write some math stuff for my c64 byt i have a little problem:
>i need a 16 bits * 16 bits multiplication program and 16 bits / 8 bits or 8 bits / 8 bits division program that will return not the integer and fractional part of the number. I dont expect ANY optimized solutions i just wan a basic version that is slow but works. Can any one help me? Aspecialy with the division ?
>
>Kris
>
Straightforward 16-bit routines, coming right up:
*------------------------
* 16-bit Multiply routine
* ACC*AUX -> [ACC,EXT] (low,hi) 32-bit result
MULT LDA #0
STA EXT+1
LDY #$11
]LOOP LSR EXT+1
ROR
ROR ACC+1
ROR ACC
BCC MUL2
CLC
ADC AUX
PHA
LDA AUX+1
ADC EXT+1
STA EXT+1
PLA
MUL2 DEY
BNE ]LOOP
STA EXT
RTS
* Divide routine
* ACC/AUX -> ACC, remainder in EXT
DIV LDA #0
STA EXT+1
LDY #$10
]LOOP ASL ACC
ROL ACC+1
ROL
ROL EXT+1
PHA
CMP AUX
LDA EXT+1
SBC AUX+1
BCC DIV2
STA EXT+1
PLA
SBC AUX
PHA
INC ACC
DIV2 PLA
DEY
BNE ]LOOP
STA EXT
RTS
Note that I did not write these; I lifted them off the Merlin assembler disk.
They work just like you would write one: you have two numbers, A and B.
Multiply A by each power of two contained in B. They are the most efficient
implementation of this that I have seen though (if you want to make it just
a little faster, change PLA/PHA to TXA/TAX, or store ACC in X, etc.).